package com.zds.scm.dao.impl;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.zds.scm.common.dao.AbstractDAOImpl;
import com.zds.scm.common.dao.DaoException;
import com.zds.scm.dao.StudentInfoDAO;
import com.zds.scm.domain.StudentInfo;

public class StudentInfoDAOImpl extends AbstractDAOImpl<StudentInfo> implements StudentInfoDAO {
	@Override
	protected StudentInfo rowMapper(ResultSet rs) throws SQLException {
		StudentInfo studentInfo=new StudentInfo();
		try {
			studentInfo.setClassCode(rs.getString("Class_Code"));
			studentInfo.setStudentAddress(rs.getString("Student_Address"));
			studentInfo.setStudentCode(rs.getString("Student_Code"));
			studentInfo.setStudentEmail(rs.getString("Student_Email"));
			studentInfo.setStudentMobile(rs.getString("Student_Mobile"));
			studentInfo.setStudentName(rs.getString("Student_Name"));
			studentInfo.setStudentPhone(rs.getString("Student_Phone"));
			studentInfo.setStudentQQCode(rs.getString("Student_QQCode"));
			studentInfo.setStudentSex(rs.getString("Student_Sex"));
	    } catch (Exception e) {//在DAO层不处理异常,直接抛出到外层
		   logger.error("DAO层结果集映射处理错误:"+e.getMessage());
		   throw new DaoException(e.getMessage(),e);
	   }
		return studentInfo;
	}
	@Override
	public boolean delete(StudentInfo studentInfo) {
		String sql="delete from studentinfo where student_code=?";
		Object[] args=new Object[]{studentInfo.getStudentCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean insert(StudentInfo studentInfo) {
		String sql="insert into studentinfo(Class_Code,Student_Address,Student_Code,Student_Email,Student_Mobile,Student_Name,Student_Phone,Student_QQCode,Student_Sex) values(?,?,?,?,?,?,?,?,?)";
		Object[] args=new Object[]{studentInfo.getClassCode(),studentInfo.getStudentAddress(),
				                   studentInfo.getStudentCode(),studentInfo.getStudentEmail(),
				                   studentInfo.getStudentMobile(),studentInfo.getStudentName(),
				                   studentInfo.getStudentPhone(),studentInfo.getStudentQQCode(),
				                   studentInfo.getStudentSex()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean update(StudentInfo studentInfo) {
		String sql="update studentinfo set Class_Code=?,Student_Address=?,Student_Email=?,Student_Mobile=?,Student_Name=?,Student_Phone=?,Student_QQCode=?,Student_Sex=? where student_code=?";
		Object[] args=new Object[]{studentInfo.getClassCode(),studentInfo.getStudentAddress(),
                                   studentInfo.getStudentEmail(),studentInfo.getStudentMobile(),
                                   studentInfo.getStudentName(),studentInfo.getStudentPhone(),
                                   studentInfo.getStudentQQCode(),studentInfo.getStudentSex(),
                                   studentInfo.getStudentCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public List<StudentInfo> findAll() {
		return findAll(-1,-1);
	}
	@Override
	public List<StudentInfo> findAll(int currentPage, int pageSize) {
		String sql="select * from studentinfo";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRows() {
		String sql="select count(*) from studentinfo";
		Object[] args=new Object[]{};
		return super.findEntityCount(sql, args);
	}
	@Override
	public StudentInfo findById(Serializable id) {
		String sql="select * from studentinfo where student_code=?";
		Object[] args=new Object[]{id};
		return super.findEntity(sql, args);
	}
	@Override
	public List<StudentInfo> findByClassCode(String classCode,int currentPage,int pageSize) {
		String sql="select * from studentinfo where class_code=?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{classCode};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{classCode,(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsClassCode(String classCode){
		String sql="select count(*) from studentinfo where class_code=?";
		Object[] args=new Object[]{classCode};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<StudentInfo> findByName(String likeStudentName,int currentPage,int pageSize) {
		String sql="select * from studentinfo where student_name like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeStudentName+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeStudentName+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByName(String likeStudentName){
		String sql="select count(*) from studentinfo where student_name like ?";
		Object[] args=new Object[]{"%"+likeStudentName+"%"};
		return super.findEntityCount(sql, args);
	}
	
	@Override
	public List<StudentInfo> findByCode(String likeStudentCode,int currentPage,int pageSize) {
		String sql="select * from studentinfo where student_name like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeStudentCode+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeStudentCode+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByCode(String likeStudentCode){
		String sql="select count(*) from studentinfo where student_code like ?";
		Object[] args=new Object[]{"%"+likeStudentCode+"%"};
		return super.findEntityCount(sql, args);
	}
}
